package com.goodving.guli.server;

import android.content.Context;
import android.text.TextUtils;
import android.util.Base64;

import com.goodving.guli.bt.Utils;
import com.goodving.guli.bt.model.BasicDateData;
import com.goodving.guli.bt.model.Friend;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.profile.scale.C3515h;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.model.LoginInfo;
import com.goodving.guli.model.SyncedServerDataInfo;
import com.goodving.guli.server.sync.SyncServerData;
import com.goodving.guli.view.C3657a;
import com.goodving.guli.view.C4220a;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;


/* renamed from: cn.com.smartdevices.bracelet.j.m */
public class WebRes { // C1330m
    private static String f6821a;

    static {
        f6821a = "WebRes";
    }

    public static WebResponse m11643a(Context context, int i, String str, ArrayList<BasicDateData> arrayList) {
        WebResponse a;
        WebResponse c1331n = new WebResponse();
        try {
            GuliLog.m12360b(f6821a, "parseDownload:\n" + str);
            JSONObject jSONObject = new JSONObject(str);
            a = WebResponse.getWebResponse(context, jSONObject);
            try {
                if (a.m11659c()) {
                    JSONArray jSONArray;
                    JSONObject jSONObject2 = jSONObject.getJSONObject("data");
                    if (!jSONObject2.isNull(SyncServerData.f8329o)) {
                        jSONArray = jSONObject2.getJSONArray(SyncServerData.f8329o);
                        SyncedServerDataInfo syncedServerDataInfo = new SyncedServerDataInfo(i);
                        if (jSONArray.length() == 2) {
                            syncedServerDataInfo.setState(2);
                            syncedServerDataInfo.setStartDate(jSONArray.getString(0));
                            syncedServerDataInfo.setStopDate(jSONArray.getString(1));
                        } else {
                            syncedServerDataInfo.setState(1);
                        }
                        syncedServerDataInfo.saveInfo();
                    }
                    jSONArray = jSONObject2.getJSONArray("list");
                    int length = jSONArray.length();
                    SyncedServerDataInfo syncedServerDataInfo2 = new SyncedServerDataInfo(i);
                    if (length > 0) {
                        syncedServerDataInfo2.setState(2);
                        syncedServerDataInfo2.setStartDate(jSONArray.getJSONObject(0).getString("date"));
                        syncedServerDataInfo2.setStopDate(jSONArray.getJSONObject(length - 1).getString("date"));
                    } else {
                        syncedServerDataInfo2.setState(1);
                    }
                    syncedServerDataInfo2.saveInfo();
                    arrayList.ensureCapacity(length);
                    for (int i2 = 0; i2 < length; i2++) {
                        arrayList.add(WebRes.m11646a(jSONArray.getJSONObject(i2), 0));
                    }
                }
            } catch (JSONException e) {
                e = e;
                a.f6829h = 2;
                GuliLog.m12370d(f6821a, e.getMessage());
                return a;
            }
        } catch (JSONException e2) {
            JSONException e3;
            JSONException jSONException = e2;
            a = c1331n;
            e3 = jSONException;
            a.f6829h = 2;
            GuliLog.m12370d(f6821a, e3.getMessage());
            return a;
        }
        return a;
    }

    public static WebResponse m11644a(Context context, String str, PersonInfo personInfo) {
        WebResponse a;
        JSONException e;
        WebResponse c1331n = new WebResponse();
        try {
            JSONObject jSONObject = new JSONObject(str);
            a = WebResponse.getWebResponse(context, jSONObject);
            try {
                if (a.m11659c()) {
                    JSONObject jSONObject2 = jSONObject.getJSONObject("data");
                    if (jSONObject2 != null) {
                        String optString = jSONObject2.optString("avatar");
                        if (optString.length() > 0) {
                            personInfo.avatarUrl = optString;
                        }
                        GuliLog.tracePrintInfo(f6821a, "get avatar Url: " + optString);
                    } else {
                        a.f6829h = 2;
                    }
                }
            } catch (JSONException e2) {
                e = e2;
                a.f6829h = 2;
                GuliLog.tracePrintInfo(f6821a, e.getMessage());
                return a;
            }
        } catch (JSONException e3) {
            JSONException jSONException = e3;
            a = c1331n;
            e = jSONException;
            a.f6829h = 2;
            GuliLog.tracePrintInfo(f6821a, e.getMessage());
            return a;
        }
        return a;
    }

    public static WebResponse m11645a(Context context, String str, ArrayList<PersonInfo> arrayList) {
        WebResponse a;
        GuliLog.m12376f(f6821a, "parseFriendList:" + str);
        WebResponse c1331n = new WebResponse();
        try {
            JSONObject jSONObject = new JSONObject(str);
            a = WebResponse.getWebResponse(context, jSONObject);
            try {
                if (a.m11659c()) {
                    JSONArray jSONArray = jSONObject.getJSONObject("data").getJSONArray("list");
                    int length = jSONArray.length();
                    arrayList.ensureCapacity(length);
                    for (int i = 0; i < length; i++) {
                        PersonInfo a2 = WebRes.m11649a(jSONArray.getJSONObject(i));
                        arrayList.add(a2);
                        GuliLog.tracePrintInfo(f6821a, a2.toString());
                    }
                }
            } catch (Exception e) {
                e = e;
                a.f6829h = 2;
                GuliLog.tracePrintInfo(f6821a, e.getMessage());
                return a;
            }
        } catch (Exception e2) {
            Exception e3;
            Exception exception = e2;
            a = c1331n;
            e3 = exception;
            a.f6829h = 2;
            GuliLog.tracePrintInfo(f6821a, e3.getMessage());
            return a;
        }
        return a;
    }

    private static BasicDateData m11646a(JSONObject jSONObject, int i) {
        BasicDateData basicDateData = new BasicDateData();
        try {
            basicDateData.date = jSONObject.getString("date");
            basicDateData.summary = jSONObject.getString("summary");
        } catch (JSONException e) {
            //TBD
        }
        basicDateData.type = i;
        try {
            if (!jSONObject.isNull("source")) {
                String string = jSONObject.getString("source");
                if (!TextUtils.isEmpty(string)) {
                    basicDateData.source = Integer.valueOf(string).intValue();
                }
            }
            if (!jSONObject.isNull("data")) {
                basicDateData.data = Base64.decode(jSONObject.getString("data"), 2);
            }
            if (!jSONObject.isNull("data_hr")) {
                basicDateData.dataHR = Base64.decode(jSONObject.getString("data_hr"), 2);
            }
            if (!jSONObject.isNull("summary_hr")) {
                basicDateData.summaryHR = jSONObject.getString("summary_hr");
            }
        } catch (JSONException e) {
            //TBD
        }
        return basicDateData;
    }

    public static LoginInfo m11647a(LoginInfo loginInfo, String str) {
        LoginInfo loginInfo2 = loginInfo == null ? new LoginInfo() : loginInfo;
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optString("result").equals("ok")) {
                jSONObject = jSONObject.optJSONObject("data");
                if (jSONObject != null) {
                    loginInfo2.aliasNick = jSONObject.optString(ClientDataConstants.PARAM_ALIAS_NICK);
                    loginInfo2.miliaoNick = jSONObject.optString(ClientDataConstants.PARAM_MILIAO_NICK);
                    loginInfo2.userid = jSONObject.optLong(ClientDataConstants.PARAM_XIAOMI_USER_ID);
                    loginInfo2.miliaoIcon = jSONObject.optString(ClientDataConstants.PARAM_MILIAO_ICON);
                    loginInfo2.miliaoIcon_320 = jSONObject.optString(ClientDataConstants.PARAM_MILIAO_ICON_320);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return loginInfo2;
    }

    public static PersonInfo m11648a(String str, PersonInfo personInfo) {
        try {
            WebRes.m11652a(new JSONObject(Utils.m8064e(str)), personInfo);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return personInfo;
    }

    private static PersonInfo m11649a(JSONObject jSONObject) {
        PersonInfo personInfo = new PersonInfo();
        try {
            if (jSONObject.has(C4220a.f20891n) && jSONObject.getString(C4220a.f20891n).length() > 0) {
                personInfo.uid = jSONObject.getLong(C4220a.f20891n);
            }
            personInfo.nickname = jSONObject.getString(Friend.f7787c);
            if (jSONObject.has(ClientDataConstants.PARAM_BIND_GENDER) && jSONObject.getString(ClientDataConstants.PARAM_BIND_GENDER).length() > 0) {
                personInfo.gender = jSONObject.getInt(ClientDataConstants.PARAM_BIND_GENDER);
            }
            if (jSONObject.has(ClientDataConstants.PARAM_BIND_HEIGHT) && jSONObject.getString(ClientDataConstants.PARAM_BIND_HEIGHT).length() > 0) {
                personInfo.height = jSONObject.getInt(ClientDataConstants.PARAM_BIND_HEIGHT);
            }
            if (jSONObject.has(Friend.f7788d) && jSONObject.getString(Friend.f7788d).length() > 0) {
                personInfo.weight = (float) jSONObject.getInt(Friend.f7788d);
            }
            personInfo.avatarUrl = jSONObject.getString("avatar");
            personInfo.birthday = jSONObject.getString(ClientDataConstants.PARAM_BIND_BIRTHDAY);
            String[] split = personInfo.birthday.split("-");
            int i = Calendar.getInstance().get(Calendar.YEAR);
            if (split[0] != null && split[0].length() > 0) {
                personInfo.age = i - Integer.parseInt(split[0]);
            }
//        personInfo.location = UserLocationData.fromJsonStr(jSONObject.optString(LocationManagerProxy.KEY_LOCATION_CHANGED));
//        personInfo.source = jSONObject.getString("source");
//        if (jSONObject.has("status") && jSONObject.getString("status").length() > 0) {
//            personInfo.state = jSONObject.getInt("status");
//        }
//        if (jSONObject.has("gid") && jSONObject.getString("gid").length() > 0) {
//            personInfo.gid = jSONObject.getInt("gid");
//        }
            personInfo.personSignature = jSONObject.getString("signature");
        } catch (JSONException e) {
            //TBD
        }
        personInfo.pinyin = C3657a.f18328f;
        return personInfo;
    }

    public static String m11650a(Context context, String str) {
        String str2 = null;
        GuliLog.m12376f(f6821a, "parseWeixinQR:" + str);
        WebResponse c1331n = new WebResponse();
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (WebResponse.getWebResponse(context, jSONObject).m11659c()) {
                jSONObject = jSONObject.getJSONObject("data");
                if (jSONObject != null) {
                    str2 = jSONObject.getString("list");
                }
            }
        } catch (JSONException e) {
            GuliLog.tracePrintInfo(f6821a, e.getMessage());
        }
        return str2;
    }

    public static String m11651a(String str) {
        if (str == null) {
            return C3657a.f18328f;
        }
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (!jSONObject.optString("result").equals("ok")) {
                return C3657a.f18328f;
            }
            jSONObject = jSONObject.optJSONObject("data");
            if (jSONObject != null) {
                return jSONObject.optString("phone");
            }
            return C3657a.f18328f;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return C3657a.f18328f;
    }

    private static void m11652a(JSONObject jSONObject, PersonInfo personInfo) {
        GuliLog.tracePrintInfo(f6821a, "parseUserInfo dataObj:" + jSONObject.toString());
        if (jSONObject.length() == 0) {
            GuliLog.tracePrintInfo(f6821a, "parseUserInfo dataObj is null");
            return;
        }
        try {
            if (jSONObject.has(C4220a.f20891n) && jSONObject.getString(C4220a.f20891n).length() > 0) {
                personInfo.uid = jSONObject.getLong(C4220a.f20891n);
            }
            if (personInfo.getNeedSyncServer() != 0) {
                GuliLog.tracePrintInfo(f6821a, "\n Person info not synced to server, so we skip parse from server! \n");
                return;
            }
            if (jSONObject.has(ClientDataConstants.PARAM_BIND_HEIGHT) && jSONObject.getString(ClientDataConstants.PARAM_BIND_HEIGHT).length() > 0) {
                personInfo.height = jSONObject.getInt(ClientDataConstants.PARAM_BIND_HEIGHT);
            }
            if (jSONObject.has(Friend.f7788d) && jSONObject.getString(Friend.f7788d).length() > 0) {
                int i = jSONObject.getInt(Friend.f7788d);
                if (i > C3515h.f17660E) {
                    personInfo.weight = new BigDecimal((double) (((float) i) / 1000.0f)).setScale(1, 4).floatValue();
                } else {
                    personInfo.weight = (float) i;
                }
            }
            if (jSONObject.has("weight_float") && jSONObject.getString("weight_float").length() > 0) {
                personInfo.weight = (float) jSONObject.getDouble("weight_float");
            }
            if (jSONObject.has(ClientDataConstants.PARAM_BIND_GENDER) && jSONObject.getString(ClientDataConstants.PARAM_BIND_GENDER).length() > 0) {
                personInfo.gender = jSONObject.getInt(ClientDataConstants.PARAM_BIND_GENDER);
            }
            if (jSONObject.has(ClientDataConstants.f6754y) && jSONObject.optString(ClientDataConstants.f6754y).length() > 0) {
                personInfo.nickname = jSONObject.getString(ClientDataConstants.f6754y);
            }
            personInfo.lastLoginTime = jSONObject.getString("last_login_time");
            personInfo.createTime = jSONObject.getString("creat_time");
            personInfo.birthday = jSONObject.getString(ClientDataConstants.PARAM_BIND_BIRTHDAY);
            personInfo.avatarUrl = jSONObject.getString("avatar");
//        UserLocationData fromJsonStr = UserLocationData.fromJsonStr(jSONObject.optString(LocationManagerProxy.KEY_LOCATION_CHANGED));
//        if (fromJsonStr != null && fromJsonStr.isValid()) {
//            personInfo.location = fromJsonStr;
//        }
//        MiliConfig fromJsonStr2 = MiliConfig.fromJsonStr(Utils.m8068f(jSONObject.optString(C1493i.f7517e)));
//        if (fromJsonStr2 != null && fromJsonStr2.isValid()) {
//            personInfo.setMiliConfig(fromJsonStr2);
//            GuliLog.m12370d(f6821a, "miliConfig=" + fromJsonStr2);
//        }
//        ArrayList parseAlarmClockItems = PersonInfo.parseAlarmClockItems(Utils.m8068f(jSONObject.optString(ClientDataConstants.PARAM_BIND_PERSON_ALARM_CLOCK)));
//        if (parseAlarmClockItems != null) {
//            personInfo.alarmClockItems = parseAlarmClockItems;
//        }
            personInfo.age = jSONObject.optInt(ClientDataConstants.am);
            personInfo.setVersion(jSONObject.optInt("version"));
            personInfo.personSignature = jSONObject.getString("signature");
        }catch (JSONException e) {
            //TBD
        }
    }

    public static LoginInfo m11653b(LoginInfo loginInfo, String str) {
        try {
            JSONObject jSONObject = new JSONObject(str);
            if (jSONObject.optString("result").equals("ok")) {
                jSONObject = jSONObject.optJSONObject("data");
                if (jSONObject != null) {
                    loginInfo.friends = jSONObject.optString(ClientDataConstants.PARAM_MILIAO_FRIENDS);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return loginInfo;
    }
}
